package com.tihi.util;

import java.io.File;
import java.math.RoundingMode;
import java.text.DecimalFormat;

import android.content.Context;
import android.graphics.Point;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tihi.handsuiviewer.App;
import com.tihi.handsuiviewer.R;

/**
 * 
 * @author duantihi
 *
 * @date 2016-2-1 上午9:19:57
 */
public class AppUtils {
	
	public static void toast(String msg) {
		Toast.makeText(App.instance(), msg, Toast.LENGTH_LONG).show();
	}
	
	public static void toast(int msgResId, int duration) {
		Toast.makeText(App.instance(), msgResId, duration).show();
	}
	
	public static void toastExit() {
		View layout = LayoutInflater.from(App.instance()).inflate(R.layout.exit_toast_view, null);  
        Toast toast = new Toast(App.instance());  
//        toast.setGravity(Gravity.LEFT|Gravity.BOTTOM, 0, 0);
        
        toast.setGravity(Gravity.LEFT|Gravity.BOTTOM, 0, 0);  
//        toast.setDuration(Toast.LENGTH_LONG);  
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);  
        toast.show();  
	}
	

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 文件是否存在
	 * @param fpath
	 * @return
	 */
	public static boolean isFileExist(String fpath) {
		try {
			return isFileExist(new File(fpath));
		} catch (Exception e) {
			return false;
		}
	}
	
	public static boolean isFileExist(File file) {
		if(file != null ){
			return file.exists();
		}else {
			return false;
		}
	}
	
	public static void delFile(String fPath) {
		try {
			File file = new File(fPath);
			if(isFileExist(file)) {
				file.delete();
			}
		} catch (Exception e) {
		}
	}
	
	/**
	 * 创建目录
	 * @param dirP
	 */
	public static void newDir(String dirP) {
		File dir = new File(dirP);
		if(!dir.exists()) {
			dir.mkdirs();
		}
	}
	
	/**
	 * 保留一位小数
	 * @param price
	 * @return
	 */
	public static String formatFloat(float price){
		double pri = Double.parseDouble(String.valueOf(price));
		DecimalFormat df = new DecimalFormat("#.#");
		df.setRoundingMode(RoundingMode.FLOOR);
		return df.format(pri);
	}
	
	/**
	 * 获取屏幕分辨率
	 * @return
	 */
	public static Point getScreenSize() {
		WindowManager wm = (WindowManager) App.instance().getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		Point point = new Point(display.getWidth(), display.getHeight());
		return point;
	}
	
	public static DisplayMetrics getDeviceDisplay() {
		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager wm = (WindowManager) App.instance().getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(metrics);
		return metrics;
	}
	
	public static String getDeviceInfo() {
	    try{
	      org.json.JSONObject json = new org.json.JSONObject();
	      android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) App.instance()
	          .getSystemService(Context.TELEPHONY_SERVICE);

	      String device_id = tm.getDeviceId();

	      android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) App.instance().getSystemService(Context.WIFI_SERVICE);

	      String mac = wifi.getConnectionInfo().getMacAddress();
	      json.put("mac", mac);

	      if( TextUtils.isEmpty(device_id) ){
	        device_id = mac;
	      }

	      if( TextUtils.isEmpty(device_id) ){
	        device_id = android.provider.Settings.Secure.getString(App.instance().getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
	      }

	      json.put("device_id", device_id);

	      return json.toString();
	    }catch(Exception e){
	      e.printStackTrace();
	    }
	  return null;
	}
}
